package action.manager;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.dao.LeagueModel;
import model.dao.ManagerModel;
import model.dto.League;
import model.dto.Manager;
import utility.DateTool;
import utility.Uploader;
import controller.Action;
import controller.ActionForward;

public class ActionUpdateManager implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		
		
		Uploader up=new Uploader(request, "upload"+File.separator+"manager");
		//Get parameter
		int manager_id=Integer.parseInt(up.getMutilPathRequest().getParameter("manager_id"));
		boolean hasImageFile=up.upload( ""+manager_id);
		int country_id=Integer.parseInt(up.getMutilPathRequest().getParameter("country_id"));
		String manager_name=up.getMutilPathRequest().getParameter("manager_name");
		String manager_image=up.getMutilPathRequest().getParameter("manager_image");
			String day=up.getMutilPathRequest().getParameter("day");
			String month=up.getMutilPathRequest().getParameter("month");
			String year=up.getMutilPathRequest().getParameter("year");
		java.sql.Date manager_date_of_birth=DateTool.getDateSQL(day+"/"+month+"/"+year, "dd/MM/yyyy");
		int page=Integer.parseInt(request.getParameter("page"));
		
		//Setting up dto
		Manager dto=new Manager();
		ManagerModel dao=new ManagerModel();
		dto.setCountryId(country_id);
		dto.setName(manager_name);
		dto.setDateOfBirth(manager_date_of_birth);
		dto.setId(manager_id);
		if(hasImageFile){
				dto.setImage(up.getPreFix()+manager_id+"."+up.getExtension());//name as sequence
		}else{
			dto.setImage(manager_image);//old name
		}
		
		//processing update
		if(dao.update(dto)){
			System.out.println("Updated successfully.");
		}else{
			System.out.println("Updating fail!!!");
		}
		
		//redirect to specific page
		HttpSession session=request.getSession();
		session.setAttribute("page", page);
		
		ActionForward forward=new ActionForward();
		forward.setPath("manager.sp");
		forward.setRedirect(true);
		return forward;

	}

}
